//
//  IGStoreVC.m
//  jingdongfang
//
//  Created by BOE on 2018/1/24.
//  Copyright © 2018年 LJL. All rights reserved.
//

#import "IGStoreVC.h"
#import "StoreCollectionViewCell.h"
#import "GoodsDetailVC.h"
#import "IGMainSearchApi.h"
#import "StoreHomeApi.h"
#import "StoreDetailApi.h"
#import "IGEmptyView.h"
#import "FindLayout.h"

@interface IGStoreVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (nonatomic, strong) NSMutableArray *goodMuArr;
@property (nonatomic, strong) UICollectionView *collect;
@property (nonatomic, strong) IGMainSearchApi *searchApi;
@property (nonatomic, assign) NSInteger page;//页数

@end

@implementation IGStoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.searchKey;
    self.page = 0;
    self.searchApi = [IGMainSearchApi new];
    self.searchApi.type = IGSearchTypeStore;
    [self.view addSubview:self.collect];
    
    [self.collect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    __weak typeof(self) weakSelf = self;
    [self.collect ig_setHeaderWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf requestData];
    }];
    
    [self requestData];
}

- (NSMutableArray *)goodMuArr {
    if (!_goodMuArr) {
        _goodMuArr = [NSMutableArray array];
    }
    return _goodMuArr;
}

- (UICollectionView *)collect {
    if (!_collect) {
        FindLayout *layout = [[FindLayout alloc] init];
        _collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:layout];
        _collect.backgroundColor = [UIColor mainBackgroundColor];
        _collect.alwaysBounceVertical = YES;
        _collect.delegate = self;
        _collect.dataSource = self;
        _collect.emptyDataSetSource = self;
        _collect.emptyDataSetDelegate = self;
        [_collect registerNib:[UINib nibWithNibName:@"StoreCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"StoreCollectionViewCell"];
    }
    return _collect;
}

- (void)requestData
{
    self.searchApi.page = self.page;
    self.searchApi.searchKey = self.searchKey;
    __weak typeof(self) weakSelf = self;
    [self.searchApi startWithDecodeClass:[IGMainSearchResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.collect ig_endHeaderRefreshing];
        [weakSelf.collect ig_endFooterRefreshing];
        if (responseObject) {
            IGMainSearchResp *resp = responseObject;
            IGMainSearchModel *model = resp.data;
            if (model.lstGood && model.lstGood.count > 0) {
                [weakSelf setupFooter];
            } else {
                [weakSelf.collect ig_endFooterRefreshingWithNoMoreData];
            }
            
            if (weakSelf.page == 0) {
                weakSelf.goodMuArr = [NSMutableArray arrayWithArray:model.lstGood];
            } else {
                [weakSelf.goodMuArr addObjectsFromArray:model.lstGood];
            }
            [weakSelf.collect reloadData];
        } else {
            //空白页处理逻辑写在这里
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.collect ig_setFooterWithRefreshingBlock:^{
        selfWeak.page++;
        [selfWeak requestData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.goodMuArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (KSCREENWIDTH - 3) / 2;
    CGFloat height = width * 360 / 372;
    return CGSizeMake(width , height + 65);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsListModel *goodsModel = _goodMuArr[indexPath.item];
    StoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCollectionViewCell" forIndexPath:indexPath];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:goodsModel.image]];
    cell.storeName.text = goodsModel.title;
    cell.priceL.text = goodsModel.price;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsListModel *goodsModel = _goodMuArr[indexPath.item];
    GoodsDetailVC *vc = [[GoodsDetailVC alloc] init];
    vc.goodsId = goodsModel.aId;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(3, 0, 3, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

#pragma mark -Empty
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    IGEmptyView *emptyView = [[IGEmptyView alloc] init];
//    emptyView.title = @"暂无数据~";
    emptyView.title = NSLocalizedString(@"No Data", nil);
    emptyView.requestApi = self.searchApi;
    return emptyView;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
